/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "CanvasPenTest.h"
#include <native_drawing/drawing_brush.h>
#include <native_drawing/drawing_filter.h>
#include <native_drawing/drawing_shadow_layer.h>
#include <native_drawing/drawing_shader_effect.h>
#include <native_drawing/drawing_canvas.h>
#include <native_drawing/drawing_pixel_map.h>
#include <native_drawing/drawing_point.h>
#include <native_drawing/drawing_record_cmd.h>
#include <native_drawing/drawing_rect.h>
#include <native_drawing/drawing_region.h>
#include <native_drawing/drawing_register_font.h>
#include <native_drawing/drawing_canvas.h>
#include <native_drawing/drawing_pen.h>
#include <native_drawing/drawing_path.h>
#include <native_drawing/drawing_pixel_map.h>
#include <native_drawing/drawing_font.h>
#include <native_color_space_manager/native_color_space_manager.h>
#include <multimedia/image_framework/image/pixelmap_native.h>

void addCanvasPenTest(std::vector<napi_property_descriptor>& descData)
{
    descData.push_back({"canvasDrawPixelMapRectConstraintSuccess", nullptr,
        OH_Drawing_CanvasDrawPixelMapRectConstraintSuccess, nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"canvasDrawPixelMapRectConstraintFail", nullptr,
        OH_Drawing_CanvasDrawPixelMapRectConstraintFail, nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"canvasDrawPointSuccess", nullptr, OH_Drawing_CanvasDrawPointSuccess,
        nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"canvasDrawPointFail", nullptr, OH_Drawing_CanvasDrawPointFail,
        nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"canvasDrawColorSuccess", nullptr, OH_Drawing_CanvasDrawColorSuccess,
        nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"canvasDrawColorFail", nullptr, OH_Drawing_CanvasDrawColorFail,
        nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"canvasDrawSingleCharacterSuccess", nullptr, OH_Drawing_CanvasDrawSingleCharacterSuccess,
        nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"canvasDrawSingleCharacterFail", nullptr, OH_Drawing_CanvasDrawSingleCharacterFail,
        nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"canvasDrawSingleCharacterWithFeaturesSuccess", nullptr,
        OH_Drawing_CanvasDrawSingleCharacterWithFeaturesSuccess, nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"canvasDrawSingleCharacterWithFeaturesFail", nullptr,
        OH_Drawing_CanvasDrawSingleCharacterWithFeaturesFail, nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"canvasClipRegionSuccess", nullptr, OH_Drawing_CanvasClipRegionSuccess,
        nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"canvasClipRegionFail", nullptr, OH_Drawing_CanvasClipRegionFail,
        nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"canvasIsClipEmptySuccess", nullptr, OH_Drawing_CanvasIsClipEmptySuccess,
        nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"canvasIsClipEmptyFail", nullptr, OH_Drawing_CanvasIsClipEmptyFail,
        nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"canvasGetImageInfoSuccess", nullptr, OH_Drawing_CanvasGetImageInfoSuccess,
        nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"canvasGetImageInfoFail", nullptr, OH_Drawing_CanvasGetImageInfoFail,
        nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"penGetAlphaFloatSucccess", nullptr, OH_Drawing_PenGetAlphaFloatSucccess,
        nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"penGetRedFloatSucccess", nullptr, OH_Drawing_PenGetRedFloatSucccess,
        nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"penGetGreenFloatSucccess", nullptr, OH_Drawing_PenGetGreenFloatSucccess,
        nullptr, nullptr, nullptr, napi_default, nullptr});
    descData.push_back({"penGetBlueFloatSuccess", nullptr, OH_Drawing_PenGetBlueFloatSuccess,
        nullptr, nullptr, nullptr, napi_default, nullptr});
}
napi_value OH_Drawing_CanvasDrawPixelMapRectConstraintSuccess(napi_env env, napi_callback_info info)
{
    napi_value result;
    uint32_t width = 100;
    uint32_t height = 100;
    OH_Drawing_Canvas* canvas = OH_Drawing_CanvasCreate();
    OH_PixelmapNative *pixelMapNative = nullptr;
    uint8_t* data = new uint8_t[100 * 100 * 4];
    size_t dataLength = 100 * 100 * 4;
    for (int i = 0; i < dataLength; ++i) {
        data[i] = 0xFF;
    }
    OH_Pixelmap_InitializationOptions *options = nullptr;
    OH_PixelmapInitializationOptions_Create(&options);
    OH_PixelmapInitializationOptions_SetWidth(options, width);
    OH_PixelmapInitializationOptions_SetHeight(options, height);
    OH_PixelmapNative_CreatePixelmap(data, dataLength, options, &pixelMapNative);
    OH_Drawing_PixelMap *pixelMap = OH_Drawing_PixelMapGetFromOhPixelMapNative(pixelMapNative);
    float left = 10;
    float top = 10;
    float right = 10;
    float bottom = 10;
    OH_Drawing_Rect* src = OH_Drawing_RectCreate(left, top, right, bottom);
    OH_Drawing_Rect* dst = OH_Drawing_RectCreate(left, top, right, bottom);
    OH_Drawing_SamplingOptions* samplingOptions = OH_Drawing_SamplingOptionsCreate(FILTER_MODE_NEAREST,
        MIPMAP_MODE_NEAREST);
    OH_Drawing_ErrorCode errorCode =  OH_Drawing_CanvasDrawPixelMapRectConstraint(canvas, pixelMap, src, dst,
        samplingOptions, STRICT_SRC_RECT_CONSTRAINT);
    napi_create_int32(env, errorCode == OH_DRAWING_SUCCESS, &result);
    return result;
}
napi_value OH_Drawing_CanvasDrawPixelMapRectConstraintFail(napi_env env, napi_callback_info info)
{
    napi_value result;
    OH_Drawing_Canvas* canvas = nullptr;
    OH_PixelmapNative* pixelmapNative = nullptr;
    OH_PixelmapNative *pixelMapNative = nullptr;
    uint8_t data = 10;
    size_t dataLength = 1;
    OH_Pixelmap_InitializationOptions *options;
    OH_PixelmapInitializationOptions_Create(&options);
    OH_PixelmapNative_CreatePixelmap(&data, dataLength, options, &pixelMapNative);
    OH_Drawing_PixelMap *pixelMap = OH_Drawing_PixelMapGetFromOhPixelMapNative(pixelmapNative);
    float left = 10;
    float top = 10;
    float right = 10;
    float bottom = 10;
    OH_Drawing_Rect* src = OH_Drawing_RectCreate(left, top, right, bottom);
    OH_Drawing_Rect* dst = OH_Drawing_RectCreate(left, top, right, bottom);
    OH_Drawing_SamplingOptions* samplingOptions = OH_Drawing_SamplingOptionsCreate(FILTER_MODE_NEAREST,
        MIPMAP_MODE_NEAREST);
    OH_Drawing_ErrorCode errorCode =  OH_Drawing_CanvasDrawPixelMapRectConstraint(canvas, pixelMap, src, dst,
        samplingOptions, STRICT_SRC_RECT_CONSTRAINT);
    napi_create_int32(env, errorCode == OH_DRAWING_ERROR_INVALID_PARAMETER, &result);
    return result;
}

napi_value OH_Drawing_CanvasDrawPointSuccess(napi_env env, napi_callback_info info)
{
    napi_value result;

    OH_Drawing_PointMode* pm = new OH_Drawing_PointMode;
    OH_Drawing_Canvas* canvas = OH_Drawing_CanvasCreate();
    OH_Drawing_Point2D* point = new OH_Drawing_Point2D;
    OH_Drawing_ErrorCode errorCode =  OH_Drawing_CanvasDrawPoint(canvas, point);
    napi_create_int32(env, errorCode == OH_DRAWING_SUCCESS, &result);
    return result;
}

napi_value OH_Drawing_CanvasDrawPointFail(napi_env env, napi_callback_info info)
{
    napi_value result;

    OH_Drawing_PointMode* pm = new OH_Drawing_PointMode;
    OH_Drawing_Canvas* canvas = nullptr;
    OH_Drawing_Point2D* point = new OH_Drawing_Point2D;
    OH_Drawing_ErrorCode errorCode =  OH_Drawing_CanvasDrawPoint(canvas, point);
    napi_create_int32(env, errorCode == OH_DRAWING_ERROR_INVALID_PARAMETER, &result);
    return result;
}

napi_value OH_Drawing_CanvasDrawColorSuccess(napi_env env, napi_callback_info info)
{
    napi_value result;
    OH_Drawing_Canvas* canvas = OH_Drawing_CanvasCreate();
    uint32_t color = 100;
    OH_Drawing_ErrorCode errorCode =  OH_Drawing_CanvasDrawColor(canvas, color, BLEND_MODE_CLEAR);
    napi_create_int32(env, errorCode == OH_DRAWING_SUCCESS, &result);
    return result;
}

napi_value OH_Drawing_CanvasDrawColorFail(napi_env env, napi_callback_info info)
{
    napi_value result;
    uint32_t color = 100;
    OH_Drawing_ErrorCode errorCode =  OH_Drawing_CanvasDrawColor(nullptr, color, BLEND_MODE_CLEAR);
    napi_create_int32(env, errorCode == OH_DRAWING_ERROR_INVALID_PARAMETER, &result);
    return result;
}

napi_value OH_Drawing_CanvasDrawSingleCharacterSuccess(napi_env env, napi_callback_info info)
{
    napi_value result;
    float x = 10;
    float y = 10;
    OH_Drawing_Canvas* canvas = OH_Drawing_CanvasCreate();
    OH_Drawing_Font* font = OH_Drawing_FontCreate();
    OH_Drawing_ErrorCode errorCode =  OH_Drawing_CanvasDrawSingleCharacter(canvas, "test", font, x, y);
    napi_create_int32(env, errorCode == OH_DRAWING_SUCCESS, &result);
    return result;
}
    
napi_value OH_Drawing_CanvasDrawSingleCharacterFail(napi_env env, napi_callback_info info)
{
    napi_value result;
    float x = 10;
    float y = 10;
    OH_Drawing_Font* font = OH_Drawing_FontCreate();
    OH_Drawing_ErrorCode errorCode =  OH_Drawing_CanvasDrawSingleCharacter(nullptr, "test", font, x, y);
    napi_create_int32(env, errorCode == OH_DRAWING_ERROR_INVALID_PARAMETER, &result);
    return result;
}
    
napi_value OH_Drawing_CanvasDrawSingleCharacterWithFeaturesSuccess(napi_env env, napi_callback_info info)
{
    napi_value result;
    float x = 10;
    float y = 10;
    OH_Drawing_Canvas* canvas = OH_Drawing_CanvasCreate();
    OH_Drawing_Font* font = OH_Drawing_FontCreate();
    OH_Drawing_FontFeatures* fontFeatures = OH_Drawing_FontFeaturesCreate();
    OH_Drawing_ErrorCode errorCode = OH_DRAWING_SUCCESS;
    errorCode = OH_Drawing_CanvasDrawSingleCharacterWithFeatures(canvas, "test", font, x, y, fontFeatures);
    napi_create_int32(env, errorCode == OH_DRAWING_SUCCESS, &result);
    return result;
}

napi_value OH_Drawing_CanvasDrawSingleCharacterWithFeaturesFail(napi_env env, napi_callback_info info)
{
    napi_value result;
    float x = 10;
    float y = 10;
    OH_Drawing_Canvas* canvas = nullptr;
    OH_Drawing_Font* font = OH_Drawing_FontCreate();
    OH_Drawing_FontFeatures* fontFeatures = OH_Drawing_FontFeaturesCreate();
    OH_Drawing_ErrorCode errorCode = OH_DRAWING_SUCCESS;
    errorCode = OH_Drawing_CanvasDrawSingleCharacterWithFeatures(canvas, "test", font, x, y, fontFeatures);
    napi_create_int32(env, errorCode == OH_DRAWING_ERROR_INVALID_PARAMETER, &result);
    return result;
}

napi_value OH_Drawing_CanvasClipRegionSuccess(napi_env env, napi_callback_info info)
{
    napi_value result;
    float x = 10;
    float y = 10;
    OH_Drawing_Canvas* canvas = OH_Drawing_CanvasCreate();
    OH_Drawing_Region* region = OH_Drawing_RegionCreate();
    OH_Drawing_ErrorCode errorCode =  OH_Drawing_CanvasClipRegion(canvas, region, DIFFERENCE);
    napi_create_int32(env, errorCode == OH_DRAWING_SUCCESS, &result);
    return result;
}

napi_value OH_Drawing_CanvasClipRegionFail(napi_env env, napi_callback_info info)
{
    napi_value result;
    float x = 10;
    float y = 10;
    OH_Drawing_Canvas* canvas = nullptr;
    OH_Drawing_Region* region = OH_Drawing_RegionCreate();
    OH_Drawing_ErrorCode errorCode = OH_Drawing_CanvasClipRegion(canvas, region, DIFFERENCE);
    napi_create_int32(env, errorCode == OH_DRAWING_ERROR_INVALID_PARAMETER, &result);
    return result;
}

napi_value OH_Drawing_CanvasIsClipEmptySuccess(napi_env env, napi_callback_info info)
{
    napi_value result;
    OH_Drawing_Canvas* canvas = OH_Drawing_CanvasCreate();
    bool isClipEmpty = true;
    OH_Drawing_ErrorCode errorCode =  OH_Drawing_CanvasIsClipEmpty(canvas, &isClipEmpty);
    napi_create_int32(env, errorCode == OH_DRAWING_SUCCESS, &result);
    return result;
}

napi_value OH_Drawing_CanvasIsClipEmptyFail(napi_env env, napi_callback_info info)
{
    napi_value result;
    OH_Drawing_Canvas* canvas = nullptr;
    bool isClipEmpty = true;
    OH_Drawing_ErrorCode errorCode =  OH_Drawing_CanvasIsClipEmpty(canvas, &isClipEmpty);
    napi_create_int32(env, errorCode == OH_DRAWING_ERROR_INVALID_PARAMETER, &result);
    return result;
}

napi_value OH_Drawing_CanvasGetImageInfoSuccess(napi_env env, napi_callback_info info)
{
    napi_value result;
    OH_Drawing_Canvas* canvas = OH_Drawing_CanvasCreate();
    OH_Drawing_Image_Info* pinfo = new OH_Drawing_Image_Info;
    OH_Drawing_ErrorCode errorCode =  OH_Drawing_CanvasGetImageInfo(canvas, pinfo);
    napi_create_int32(env, errorCode == OH_DRAWING_SUCCESS, &result);
    return result;
}

napi_value OH_Drawing_CanvasGetImageInfoFail(napi_env env, napi_callback_info info)
{
    napi_value result;
    OH_Drawing_ErrorCode errorCode =  OH_Drawing_CanvasGetImageInfo(nullptr, nullptr);
    napi_create_int32(env, errorCode == OH_DRAWING_ERROR_INVALID_PARAMETER, &result);
    return result;
}

napi_value OH_Drawing_PenGetAlphaFloatSucccess(napi_env env, napi_callback_info info)
{
    napi_value result;
    OH_Drawing_Pen* pen = OH_Drawing_PenCreate();
    float a = 10;
    float r = 11;
    float g = 12;
    float b = 13;
    ColorSpaceName colorSpaceName;
    OH_NativeColorSpaceManager* colorSpaceManager = OH_NativeColorSpaceManager_CreateFromName(ADOBE_RGB);
    OH_Drawing_PenSetColor4f(pen, a, b, r, g, colorSpaceManager);
    float resa = 0;
    OH_Drawing_ErrorCode errorCode = OH_Drawing_PenGetAlphaFloat(nullptr, &resa);
    napi_create_int32(env, errorCode == OH_DRAWING_ERROR_INVALID_PARAMETER, &result);
    return result;
}

napi_value OH_Drawing_PenGetRedFloatSucccess(napi_env env, napi_callback_info info)
{
    napi_value result;
    OH_Drawing_Pen* pen = OH_Drawing_PenCreate();
    float a = 10.0f;
    float r = 11.0f;
    float g = 12.0f;
    float b = 13.0f;
    ColorSpaceName colorSpaceName;
    OH_NativeColorSpaceManager* colorSpaceManager = OH_NativeColorSpaceManager_CreateFromName(ADOBE_RGB);
    OH_Drawing_ErrorCode errorCode = OH_Drawing_PenSetColor4f(pen, a, b, r, g, colorSpaceManager);
    float resr = 0.0f;
    errorCode = OH_Drawing_PenGetRedFloat(nullptr, &resr);
    napi_create_int32(env, errorCode == OH_DRAWING_ERROR_INVALID_PARAMETER, &result);
    return result;
}

napi_value OH_Drawing_PenGetGreenFloatSucccess(napi_env env, napi_callback_info info)
{
    napi_value result;
    OH_Drawing_Pen* pen = OH_Drawing_PenCreate();
    float a = 10;
    float r = 11;
    float g = 12;
    float b = 13;
    ColorSpaceName colorSpaceName;
    OH_NativeColorSpaceManager* colorSpaceManager = OH_NativeColorSpaceManager_CreateFromName(ADOBE_RGB);
    OH_Drawing_PenSetColor4f(pen, a, b, r, g, colorSpaceManager);
    float resg = 0;
    OH_Drawing_ErrorCode errorCode = OH_Drawing_PenGetGreenFloat(nullptr, &resg);
    napi_create_int32(env, errorCode == OH_DRAWING_ERROR_INVALID_PARAMETER, &result);
    return result;
}

napi_value OH_Drawing_PenGetBlueFloatSuccess(napi_env env, napi_callback_info info)
{
    napi_value result;
    OH_Drawing_Pen* pen = OH_Drawing_PenCreate();
    float a = 10;
    float r = 11;
    float g = 12;
    float b = 13;
    ColorSpaceName colorSpaceName;
    OH_NativeColorSpaceManager* colorSpaceManager = OH_NativeColorSpaceManager_CreateFromName(ADOBE_RGB);
    OH_Drawing_PenSetColor4f(pen, a, b, r, g, colorSpaceManager);
    float resb = 0;
    OH_Drawing_ErrorCode errorCode = OH_Drawing_PenGetBlueFloat(nullptr, &resb);
    napi_create_int32(env, errorCode == OH_DRAWING_ERROR_INVALID_PARAMETER, &result);
    return result;
}